home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxarith.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.4 KB  |  90 lines

  1. /* Copyright (C) 1990, 1993, 1994, 1996, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. #ifndef gxarith_INCLUDED
  20. #  define gxarith_INCLUDED
  21.  
  22. /*$Id: gxarith.h,v 1.2 2000/09/19 19:00:33 lpd Exp $ */
  23. /* Arithmetic macros for Ghostscript library */
  24.  
  25. /* Define an in-line abs function, good for any signed numeric type. */
  26. #define any_abs(x) ((x) < 0 ? -(x) : (x))
  27.  
  28. /* Compute M modulo N.  Requires N > 0; guarantees 0 <= imod(M,N) < N, */
  29. /* regardless of the whims of the % operator for negative operands. */
  30. int imod(P2(int m, int n));
  31.  
  32. /* Compute the GCD of two integers. */
  33. int igcd(P2(int x, int y));
  34.  
  35. /*
  36.  * Given A, B, and M, compute X such that A*X = B mod M, 0 < X < M.
  37.  * Requires: M > 0, 0 < A < M, 0 < B < M, gcd(A, M) | gcd(A, B).
  38.  */
  39. int idivmod(P3(int a, int b, int m));
  40.  
  41. /*
  42.  * Compute floor(log2(N)).  Requires N > 0.
  43.  */
  44. int ilog2(P1(int n));
  45.  
  46. /* Test whether an integral value fits in a given number of bits. */
  47. /* This works for all integral types. */
  48. #define fits_in_bits(i, n)\
  49.   (sizeof(i) <= sizeof(int) ? fits_in_ubits((i) + (1 << ((n) - 1)), (n) + 1) :\
  50.    fits_in_ubits((i) + (1L << ((n) - 1)), (n) + 1))
  51. #define fits_in_ubits(i, n) (((i) >> (n)) == 0)
  52.  
  53. /*
  54.  * There are some floating point operations that can be implemented
  55.  * very efficiently on machines that have no floating point hardware,
  56.  * assuming IEEE representation and no range overflows.
  57.  * We define straightforward versions of them here, and alternate versions
  58.  * for no-floating-point machines in gxfarith.h.
  59.  */
  60. /* Test floating point values against constants. */
  61. #define is_fzero(f) ((f) == 0.0)
  62. #define is_fzero2(f1,f2) ((f1) == 0.0 && (f2) == 0.0)
  63. #define is_fneg(f) ((f) < 0.0)
  64. #define is_fge1(f) ((f) >= 1.0)
  65. /* Test whether a floating point value fits in a given number of bits. */
  66. #define f_fits_in_bits(f, n)\
  67.   ((f) >= -2.0 * (1L << ((n) - 2)) && (f) < 2.0 * (1L << ((n) - 2)))
  68. #define f_fits_in_ubits(f, n)\
  69.   ((f) >= 0 && (f) < 4.0 * (1L << ((n) - 2)))
  70.  
  71. /*
  72.  * Define a macro for computing log2(n), where n=1,2,4,...,128.
  73.  * Because some compilers limit the total size of a statement,
  74.  * this macro must only mention n once.  The macro should really
  75.  * only be used with compile-time constant arguments, but it will work
  76.  * even if n is an expression computed at run-time.
  77.  */
  78. #define small_exact_log2(n)\
  79.  ((uint)(05637042010L >> ((((n) % 11) - 1) * 3)) & 7)
  80.  
  81. /*
  82.  * The following doesn't give rise to a macro, but is used in several
  83.  * places in Ghostscript.  We observe that if M = 2^n-1 and V < M^2,
  84.  * then the quotient Q and remainder R can be computed as:
  85.  *              Q = V / M = (V + (V >> n) + 1) >> n;
  86.  *              R = V % M = (V + (V / M)) & M = V - (Q << n) + Q.
  87.  */
  88.  
  89. #endif /* gxarith_INCLUDED */
  90.